package cn.itcast.service.targo.impl;


import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import javax.script.ScriptEngine;
import java.util.List;
import java.util.UUID;

@Service
public class FactoryServiceImpl implements FactoryService {
    @Autowired
    private FactoryDao factoryDao;

    /**
     *分页查询厂家
     */
    @Override
    public PageInfo<Factory> findByPage(FactoryExample example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Factory> list = factoryDao.selectByExample(example);
        return new PageInfo<>(list);

    }


    /**
     *根据厂家id（factory_id）查询一条厂家数据
     */
    @Override
    public Factory findById(String id) {
        return factoryDao.selectByPrimaryKey(id);
    }

    /**
     *查询所有厂家
     */
    @Override
    public List<Factory> findAll(FactoryExample example) {
        return factoryDao.selectByExample(example);
    }


    /**
     *添加厂家信息
     */
    @Override
    public void save(Factory factory) {
        factoryDao.insertSelective(factory);
    }


    /**
     *修改厂家信息
     */
    @Override
    public void update(Factory factory) {
        factoryDao.updateByPrimaryKeySelective(factory);
    }


    /**
     *删除厂家信息
     */
    @Override
    public void delete(String id) {
        factoryDao.deleteByPrimaryKey(id);
    }


    //通过工厂名称获取工厂对象
    @Override
    public Factory findByFactoryName(String factoryName) {
        FactoryExample example = new FactoryExample();
        FactoryExample.Criteria criteria = example.createCriteria();
        criteria.andFactoryNameEqualTo(factoryName);

        List<Factory> factories = factoryDao.selectByExample(example);
        return factories != null && factories.size()>0 ?factories.get(0):null;

    }

}
